package org.nfIt.common.exception;

import org.nfIt.vo.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ClossName:GlobalExceptionHandler
 * Pakage:org.nfIt.common.config
 * Description
 * <p>
 * 统一异常处理类
 *
 * @ControllerAdvice ： Controler增强器,给controller增加统一的操作和处理
 * @ExceptionHandler : 捕获controller抛出指定类型异常
 *
 * @Author L
 * @Create 2024/6/18 15:07
 * @Version 2023.1.3
 */
@ControllerAdvice
public class GlobalExceptionHandlers {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.build(null, 201, "出现了异常");
    }

    /**
     * 自定义异常
     * @param e 自定义异常处理类
     */
    @ExceptionHandler(CustomException.class)// 捕获controller抛出指定类型异常
    @ResponseBody //返回前端json数据格式
    public Result error(CustomException e) {
       return Result.build(null,e.getResultCodeEnum());
    }
}
